package com.central.scheduler.remote.processor;

import com.central.scheduler.remote.command.Command;
import com.central.scheduler.remote.utils.ChannelUtils;
import com.central.scheduler.remote.utils.Host;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

/**
 * @author Tindy
 * @date 2021/10/29
 * @describe
 *  callback channel
 */
public class NettyRemoteChannel {

    /**
     *  channel
     */
    private final Channel channel;

    /**
     *  request unique identification
     */
    private final long opaque;

    /**
     * master host
     */
    private final Host host;


    public NettyRemoteChannel(Channel channel, long opaque) {
        this.channel = channel;
        this.host = ChannelUtils.toAddress(channel);
        this.opaque = opaque;
    }

    public NettyRemoteChannel(Channel channel) {
        this.channel = channel;
        this.host = ChannelUtils.toAddress(channel);
        this.opaque = -1;
    }

    public Channel getChannel() {
        return channel;
    }

    public long getOpaque() {
        return opaque;
    }

    public Host getHost() {
        return host;
    }

    public boolean isActive(){
        return this.channel.isActive();
    }

    public ChannelFuture writeAndFlush(Command command){
        return this.channel.writeAndFlush(command);
    }

    public void close(){
        this.channel.close();
    }
}

